/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import models.Student;
import views.Add;
import views.Edit;

/**
 *
 * @author plato
 */
public class Validation {

    public boolean checkErrorAddForm(String id, ArrayList<Student> ss1, String firstName, String lastName, String address, String age, String note, Date dob, Add addView) {
        boolean check = true;
        boolean checkId = false;
        StringBuilder error = new StringBuilder("");
        if (id == null || id.equals("")) {
            addView.setErrorId(Message.IS_NULL_ID);
            error.append("0");
        } else {
            addView.setErrorId("");
            if (error.toString().contains("0")) {
                int index = error.indexOf("0");
                error.deleteCharAt(index);
            }
            checkId = isNumeric(id);
            if (!checkId) {
                addView.setErrorId(Message.IS_NOT_NUMERIC_ID);
                error.append("8");
            } else {
                if (error.toString().contains("8")) {
                    int index = error.indexOf("8");
                    error.deleteCharAt(index);
                }
            }
        }
        if (firstName == null || firstName.equals("")) {
            addView.setErrorFirstName(Message.IS_NULL_FIRSTNAME);
            error.append("1");
        } else {
            addView.setErrorFirstName("");
            if (error.toString().contains("1")) {
                int index = error.indexOf("1");
                error.deleteCharAt(index);
            }
        }
        if (lastName == null || lastName.equals("")) {
            addView.setErrorLastName(Message.IS_NULL_LASTNAME);
            error.append("2");
        } else {
            addView.setErrorLastName("");
            if (error.toString().contains("2")) {
                int index = error.indexOf("2");
                error.deleteCharAt(index);
            }
        }
        if (address == null || address.equals("")) {
            addView.setErrorAddress(Message.IS_NULL_ADDRESS);
            error.append("3");
        } else {
            addView.setErrorAddress("");
            if (error.toString().contains("3")) {
                int index = error.indexOf("3");
                error.deleteCharAt(index);
            }
        }
        if (age == null || age.equals("")) {
            addView.setErrorAge(Message.IS_NULL_AGE);
            error.append("4");
        } else {
            addView.setErrorAge("");
            if (error.toString().contains("4")) {
                int index = error.indexOf("4");
                error.deleteCharAt(index);
            }
            if (!isNumeric(age)) {
                addView.setErrorAge(Message.IS_NOT_NUMERIC_AGE);
                error.append("5");
            } else {

                if (error.toString().contains("5")) {
                    int index = error.indexOf("5");
                    error.deleteCharAt(index);
                }
            }
        }
        if (dob == null || dob.equals("")) {
            addView.setErrorDob(Message.IS_NULL_OR_ERROR_DATE);
            error.append("6");
        } else {
            addView.setErrorDob("");
            if (error.toString().contains("6")) {
                int index = error.indexOf("6");
                error.deleteCharAt(index);
            }
        }
        if (note == null || note.equals("")) {
            addView.setErrorNote(Message.IS_NULL_NOTE);
            error.append("7");
        } else {
            addView.setErrorNote("");
            if (error.toString().contains("7")) {
                int index = error.indexOf("7");
                error.deleteCharAt(index);
            }
        }
        AddToXml addToXml = new AddToXml();
        if (checkId) {
            int idTemp = Integer.parseInt(id);
            boolean checkDup = addToXml.checkDublicated(idTemp, ss1);
            if (checkDup) {
                addView.setErrorId(Message.IS_DUPLICATED_ID);
                error.append("9");
            } else {
                //addView.setId("");
                if (error.toString().contains("9")) {
                    int index = error.indexOf("9");
                    error.deleteCharAt(index);
                }
            }
        }
        if (error.toString().equals("")) {
            check = true;
        } else {
            check = false;
        }
        return check;
    }

    public boolean checkErrorAddForm(String id, ArrayList<Student> ss1,String firstName, String lastName, String address, String age, String note, Date dob, Edit addView) {
       boolean check = true;
        
        StringBuilder error = new StringBuilder("");
        
        if (firstName == null || firstName.equals("")) {
            addView.setErrorFirstName(Message.IS_NULL_FIRSTNAME);
            error.append("1");
        } else {
            addView.setErrorFirstName("");
            if (error.toString().contains("1")) {
                int index = error.indexOf("1");
                error.deleteCharAt(index);
            }
        }
        if (lastName == null || lastName.equals("")) {
            addView.setErrorLastName(Message.IS_NULL_LASTNAME);
            error.append("2");
        } else {
            addView.setErrorLastName("");
            if (error.toString().contains("2")) {
                int index = error.indexOf("2");
                error.deleteCharAt(index);
            }
        }
        if (address == null || address.equals("")) {
            addView.setErrorAddress(Message.IS_NULL_ADDRESS);
            error.append("3");
        } else {
            addView.setErrorAddress("");
            if (error.toString().contains("3")) {
                int index = error.indexOf("3");
                error.deleteCharAt(index);
            }
        }
        if (age == null || age.equals("")) {
            addView.setErrorAge(Message.IS_NULL_AGE);
            error.append("4");
        } else {
            addView.setErrorAge("");
            if (error.toString().contains("4")) {
                int index = error.indexOf("4");
                error.deleteCharAt(index);
            }
            if (!isNumeric(age)) {
                addView.setErrorAge(Message.IS_NOT_NUMERIC_AGE);
                error.append("5");
            } else {

                if (error.toString().contains("5")) {
                    int index = error.indexOf("5");
                    error.deleteCharAt(index);
                }
            }
        }
        if (dob == null || dob.equals("")) {
            addView.setErrorDob(Message.IS_NULL_OR_ERROR_DATE);
            error.append("6");
        } else {
            addView.setErrorDob("");
            if (error.toString().contains("6")) {
                int index = error.indexOf("6");
                error.deleteCharAt(index);
            }
        }
        if (note == null || note.equals("")) {
            addView.setErrorNote(Message.IS_NULL_NOTE);
            error.append("7");
        } else {
            addView.setErrorNote("");
            if (error.toString().contains("7")) {
                int index = error.indexOf("7");
                error.deleteCharAt(index);
            }
        }
        AddToXml addToXml = new AddToXml();
        
        if (error.toString().equals("")) {
            check = true;
        } else {
            check = false;
        }
        return check;
    }

    public boolean isNumeric(String str) {
        try {
            int d = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }

    public boolean validateXML(String path) {
        if (path.endsWith(".xml")) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isThisDateValid(Date dateToValidate, String dateFromat) {

        if (dateToValidate == null) {
            return false;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(dateFromat);
        //sdf.setLenient(false);
        try {

            //if not valid, it will throw ParseException
            Date date = (Date) sdf.parse(dateToValidate.toString());
            System.out.println(date);

        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }

        return true;
    }

    public Date convertToDate(String input) {
        List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>();
        dateFormats.add(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"));
        dateFormats.add(new SimpleDateFormat("M/dd/yyyy"));
        dateFormats.add(new SimpleDateFormat("dd.M.yyyy"));
        dateFormats.add(new SimpleDateFormat("M/dd/yyyy hh:mm:ss a"));
        dateFormats.add(new SimpleDateFormat("dd.M.yyyy hh:mm:ss a"));
        dateFormats.add(new SimpleDateFormat("dd.MMM.yyyy"));
        dateFormats.add(new SimpleDateFormat("dd-MMM-yyyy"));

        SimpleDateFormat formatToDisplay = new SimpleDateFormat("MMM dd, yyyy");
        Date date = null;
        if (null == input) {
            return null;
        }
        for (SimpleDateFormat format : dateFormats) {
            try {
                format.setLenient(false);
                date = format.parse(input);
            } catch (ParseException e) {
                //Shhh.. try other formats
            }
            if (date != null) {
                break;
            }
        }

        return date;
    }
}
